;
(function (call) {
    var main = {
        init: function () {
            this.initView();
            this.bindEvt();
        },
        initView: function () {
            var userInfo = $("#userInfo").data('info');
            this.userInfo = userInfo;
            $("#publishTime").val(new Date().format('dd日'));
            this.getSxrybDataDetail();
        },
        getParams: function () {
            var time = new Date();
            var nowDay = time.getDay();
            var nowTime = time.getTime();
            var oneDayeTime = 24 * 60 * 60 * 1000;
            var saturday = nowTime + (6 - nowDay) * oneDayeTime;
            var startPeriod = saturday - nowTime
            startPeriod = startPeriod / (60 * 60 * 1000)
            var endPeriod = startPeriod + 48
            var params = {
                fcstTime: new Date(time).format('yyyyMMdd080000'),
                element: 'TP,TMAX,TMIN,EDA,WEA',
                startPeriod: startPeriod,
                endPeriod: endPeriod,
                interval: 24,
                stationType: 0,
                lon: "117.21",
                lat: "39.07"
            }
            return params;
        },
        // 初始化双休日预报
        initSxrybData: function () {
            var that = this;
            var $body = $(".sxryb_table tbody").empty();
            loading.add('.cafe-container');
            var url = api.external.getGdybDataByParamData;
            $.getJSON(url, that.getParams(), function (res) {
                loading.remove();
				debugger
                if (!res.success || !res.resultData) return;
                if ($.isEmptyObject(res.resultData)) return;
                var resultData = res.resultData['117.21_39.07'];
                var now = new Date();
                var nowTime = now.getTime();
                var nowDay = now.getDay();
                var oneDayeTime = 24 * 60 * 60 * 1000;
                var saturday = nowTime + (6 - nowDay) * oneDayeTime;
                var sunday = nowTime + (7 - nowDay) * oneDayeTime;
                var arr = [saturday, sunday]

                for (var i = 0; i < arr.length; i++) {
                    var wdLi = WindUtil.coverWindDesc(resultData['EDA'][2][i]);
                    var wpLi = resultData['EDA'][3][i].replace('_','转');
                    var $tr = $(`<tr>
                    <td><input type="text" disabled value="${new Date(arr[i]).format('MM月dd日')}"></td>
                    <td><input type="text" value="${resultData['WEA'][0][i]}"></td>
                    <td><input type="text" value="${wdLi}"></td>
                    <td><input type="text" value="${wpLi}"></td>
                    <td><input type="text" value="${resultData['TMIN'][0][i]}"></td>
                    <td><input type="text" value="${resultData['TMAX'][0][i]}"></td></tr>`);
                    $body.append($tr)
                }
            })
        },
        // 获取双休日预报数据
        getSxrybDataDetail: function () {
            var that = this;
            var productInfo = $(".task-menu-list li.active").data('product');
            var time = new Date().format('yyyy-MM-dd 08:00:00')
            var params = {
                product_id: productInfo.productId,
                fortime: productInfo.fortime,
                queryTime: time
            }
            $.getJSON(api.internal.common.getProductDetail, params, function (res) {
                if (!res.isSuccess) return;
                if (JSON.stringify(res.data) == "{}") {
                    that.initSxrybData();
                } else {
                    that.getSxrybData(res.data);
                }
            })
        },
        getSxrybData: function (data) {
            var item = JSON.parse(data.data.content)
            item = item.tempData.curTagData
            $("#publishTime").val(item.i_sxrjjjryb_01.val)
            $("#proText").val(data.txtContent)
            this.buildSxryTable(JSON.parse(item.g_sxrjjjryb_02.val))

        },
        buildSxryTable: function (tableData) {
            var $body = $(".sxryb_table tbody").empty();
            for (var i = 0; i < tableData.length; i++) {
                var tr = '<tr>'
                for (var j = 0; j < tableData[i].length; j++) {
                    if (j == 0) {
                        tr += '<td><input type="text" disabled value="' + tableData[i][j] + '"></td>'
                    } else {
                        tr += '<td><input type="text" value="' + tableData[i][j] + '"></td>'
                    }
                }
                tr += '</tr>'
                $body.append(tr);
            }
        },
        // 发布双休日预报
        publishSxrybData: function () {
            var that = this;
            var productInfo = $(".task-menu-list li.active").data('product');
            var dataVal = [];
            $(".sxryb_table tbody tr").each(function (i, item) {
                var list = [];
                $(item).find('td').each(function () {
                    list.push($(this).find('input').val());
                })
                dataVal.push(list)
            })
            dataVal = JSON.stringify(dataVal);
            var tempData = {
                "tempData": {
                    "curTagData": {
                        "g_sxrjjjryb_02": {
                            "val": dataVal
                        },
                        "i_sxrjjjryb_01": {
                            "val": $("#publishTime").val()
                        }
                    }
                }
            }
            tempData = JSON.stringify(tempData)
            var params = {
                product_id: productInfo.productId,
                fortime: productInfo.fortime,
                forecasterId: that.userInfo.smuser.iid,
                jsonStr: tempData,
				taskId: productInfo.taskId
            }
            $.post(api.internal.sxr.pushforeSXR, params, function (res) {
                if (res.isSuccess) {
                    alert("发布成功");
                    that.getSxrybDataDetail();
                    CafeJS.menuFunc.setTask();
                } else {
                    alert("发布失败");
                }
            })
        },
        bindEvt: function () {
            var that = this;
            $("#publishBtn").click(function () {
                that.publishSxrybData();
            })
            $("#returnBtn").click(function () {
                that.initSxrybData();
            })
        },
    };
    if (typeof call === 'function') {
        call(main);
    }
})(function (main) {
    main.init();
})